package beanstao.util.type.base;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;

import beanstao.util.exception.technical.util.reflect.FailToCastObjectException;
import beanstao.util.type.Castor;
import beanstao.util.type.Castors;

@SuppressWarnings({ "rawtypes" })
public class Collection2Array extends Castor<Collection, Object>
{

	public Collection2Array()
	{
		fromClass = Collection.class;
		toClass = Array.class;
	}

	@Override
	public Object cast(Collection src, Class<?> toType, String... args) throws FailToCastObjectException
	{
		Class<?> compType = toType.getComponentType();
		Object ary = Array.newInstance(compType, src.size());
		int index = 0;
		for (Iterator it = src.iterator(); it.hasNext();)
			Array.set(ary, index++, Castors.me().castTo(it.next(), compType));
		return ary;
	}

}
